import { Menu, Icon } from 'antd';
import * as React from 'react';
import { ClickParam } from 'antd/lib/menu';
import { connect } from 'dva';

import { history } from '../../util';

const SubMenu = Menu.SubMenu;

@connect((menu: any[]) => menu)
class LeftMenu extends React.PureComponent<any, object> {
    // private static history: any;
    constructor(props: any) {
        super(props);
        // history = props.history;
    }

    public render() {
        const { menu } = this.props;
        // console.log(LeftMenu.history);
        return (
            <Menu onClick={e => this.handleClick(e)} mode="inline" defaultSelectedKeys={[menu.defaultKey]}>
                {
                    this.renderMenu(menu.items)
                }
            </Menu>
        );
    }
    private handleClick(e: ClickParam) {
        history.push(e.key);
    }
    private renderMenu(menus: any[]) {
        return menus.map((m: any) => m.child
            ? <SubMenu key={m.key} title={<span><Icon type={m.icon} /><span>{m.text}</span></span>}>{this.renderMenu(m.child)}</SubMenu>
            : <Menu.Item key={m.key}><Icon type={m.icon} /><span>{m.text}</span></Menu.Item>);
    }
}

export default LeftMenu;
